
use std::io::{BufReader,BufRead};
use std::io::{BufWriter, Write};
use std::fs::File;

use std::collections::{HashMap as Map};

use common::Instance;
use common::Repr;

pub fn save_parameters(m:&Map<u32,Repr>,location:&str) {
    let file = match File::create(location) {
        Ok(file) =>file,
        Err(..) => panic!("room"),
    };

    let mut r = BufWriter::new(&file);

    for (p,repr) in m {
        let line = format!("{:?}",repr.v);
        write!(r,"{}: {} \n", p,line).unwrap();
    }
}

pub fn get_instances(location:&str) -> Vec<Instance> {

    let file = match File::open(location) {
        Ok(file) =>file,
        Err(..) =>panic!("room"),
    };

    let r = BufReader::new(&file);

    let mut es:Vec<Instance> = Vec::new();

    for line in r.lines() {

        let line = line.unwrap();
        let ss:Vec<&str> = line.trim().split(' ').collect();
        let xs:Vec<u32> = ss.iter().map(|s|s.parse::<u32>().unwrap()).collect();
        let instance = Instance {
            p:xs[0],
            q:xs[1],
            r:xs[2] as f64,
        };
        es.push(instance);

    }

    return es;
}
